package com.gmlive.common.sample.http

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.gmlive.common.sample.R
import com.inkegz.network.BaseModel
import com.inkegz.network.RetrofitManager
import java.lang.IllegalArgumentException

/**
 * retrofit 接入示例
 */
class RetrofitActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_retrofit)
        RetrofitManager.apply {
            changeHost(mapOf("APP_HOST" to "https://testservice.hnyapu.cn", "UPLOAD_HOST" to "http://upload.hnyapu.cn"), "APP_HOST")
            errorHandler = {
                Toast.makeText(this@RetrofitActivity, "网络不给力", Toast.LENGTH_SHORT).show()
            }
            successStrategy = { result ->
                if (result is BaseModel<*>) {
                    result.errorCode == 0 || null != result.data
                } else
                    true
            }
        }

        RetrofitManager.req(
                TestService::class.java,
                { it.apiResourceGift() },
                {
                    throw IllegalArgumentException()
                }
        )
    }
}